package com.example.bingobox.ui.schedule;

import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.bingobox.R;
import com.example.bingobox.databinding.FragmentScheduleBinding;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.example.bingobox.model.Task;
import com.example.bingobox.model.TodoBoxData;
import com.example.bingobox.adapter.TimeLabelAdapter;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;
import android.app.TimePickerDialog;
import android.widget.TimePicker;
import com.example.bingobox.model.Task.TimeLabel;
import java.nio.file.Files;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import java.lang.reflect.Type;
import android.app.DatePickerDialog;
import com.example.bingobox.view.ClockTimeSelector;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.stream.Collectors;
import com.google.gson.JsonSyntaxException;

public class ScheduleFragment extends Fragment {

    // 添加常量定义
    public static final int SHOW_INCOMPLETE = 0;  // 未完成
    public static final int SHOW_COMPLETED = 1;   // 已完成
    public static final int SHOW_EXPIRED = 2;     // 已过期
    public static final int SHOW_SEARCH = 3;      // 搜索

    private FragmentScheduleBinding binding;
    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private final Gson gson = new Gson();
    private List<TaskListFragment> fragments = new ArrayList<>();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                           ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentScheduleBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        // 使用 binding 获取视图引用
        viewPager = binding.viewPager;
        tabLayout = binding.tabLayout;

        // 设置适配器
        FragmentStateAdapter pagerAdapter = new FragmentStateAdapter(this) {
            @Override
            public int getItemCount() {
                return 4;  // 修改为4个标签页
            }

            @Override
            public Fragment createFragment(int position) {
                TaskListFragment fragment;
                switch (position) {
                    case 0:
                        fragment = TaskListFragment.newInstance(SHOW_INCOMPLETE);
                        break;
                    case 1:
                        fragment = TaskListFragment.newInstance(SHOW_COMPLETED);
                        break;
                    case 2:
                        fragment = TaskListFragment.newInstance(SHOW_EXPIRED);
                        break;
                    case 3:
                        fragment = TaskListFragment.newInstance(SHOW_SEARCH);
                        break;
                    default:
                        return null;
                }
                fragments.add(fragment);
                return fragment;
            }
        };
        viewPager.setAdapter(pagerAdapter);

        // 设置 TabLayout
        new TabLayoutMediator(tabLayout, viewPager,
            (tab, position) -> {
                switch (position) {
                    case 0:
                        tab.setText("未完成");
                        break;
                    case 1:
                        tab.setText("已完成");
                        break;
                    case 2:
                        tab.setText("已过期");
                        break;
                    case 3:
                        tab.setText("搜索");
                        break;
                }
            }
        ).attach();

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    // 添加刷新方法
    public void refreshAllFragments() {
        for (TaskListFragment fragment : fragments) {
            fragment.loadTasks();
        }
    }
} 